﻿collegeApp.factory('CollegeService', function ($http, $q) {
    var colleges = [];
    var ratings = [];
    
    var getColleges = function()
    {
        var deferred = $q.defer();
        $http.get("/api/College").success(function (data) {
            colleges.length = 0;
            for (var c in data) {
                colleges.push(data[c]);
            }

            deferred.resolve(colleges);
        }).error(function (data, status) {
            console.log(status);
            deferred.reject();
        });
        return deferred.promise;
    }

    var getReviews = function (id) {
        var deferred = $q.defer();
        $http.get("/api/Rating/"+id).success(function (data) {
            ratings.length = 0;
            for (var r in data) {
                ratings.push(data[r]);
            }

            deferred.resolve(ratings);
        }).error(function (data, status) {
            console.log(status);
            deferred.reject();
        });
        return deferred.promise;
    }

    var getCollegeReviews = function (id) {
        var collegeRatings = [];
        for (var i = 0; i < ratings.length; i++) {
            var rating = ratings[i];
            if (rating.CollegeId == id)
            {
                collegeRatings.push(rating);
            }
        }
        return collegeRatings;
    }

    var getCollege = function(id)
    {
        
        for(var i = 0; i < colleges.length; i++ )
        {
            var college = colleges[i];
            if (college.CollegeId == id)
                return college;
        }
        return null;
    }
    //var getCollege = function(id)
    //{
    //    var deferred = $q.defer();
    //    $http.get("/api/College/+id").success(function (data) {
    //        colleges.length = 0;
            
    //        deferred.resolve(data);
    //    }).error(function (data, status) {
    //        console.log(status);
    //        deferred.reject();
    //    });
    //    return deferred.promise;
    //}

    var addCollege = function (college) {
        var deferred = $q.defer();
        $http.post("/api/College", college).success(function (data) {
            //colleges.length = 0;
            
            deferred.resolve();
        }).error(function (data, status) {
            console.log(status);
            deferred.reject();
        });
        return deferred.promise;
    }

    var saveCollege = function (college) {
        var deferred = $q.defer();
        $http.put("/api/College", college).success(function (data) {
            //colleges.length = 0;

            deferred.resolve();
        }).error(function (data, status) {
            console.log(status);
            deferred.reject();
        });
        return deferred.promise;
    }

    var deleteCollege = function (id) {
        var deferred = $q.defer();
        $http.delete("/api/College/"+id).success(function (data) {
            //colleges.length = 0;

            deferred.resolve();
        }).error(function (data, status) {
            console.log(status);
            deferred.reject();
        });
        return deferred.promise;
    }

    var addReview = function (review) {
        var deferred = $q.defer();
        $http.post("/api/Rating", review).success(function (data) {
            //colleges.length = 0;

            deferred.resolve();
        }).error(function (data, status) {
            console.log(status);
            deferred.reject();
        });
        return deferred.promise;
    }


    // return function pointers here
    return {
        getColleges: getColleges,
        addCollege: addCollege,
        deleteCollege: deleteCollege,
        getCollege: getCollege,
        saveCollege: saveCollege,
        addReview: addReview,
        getReviews: getReviews,
        getCollegeReviews: getCollegeReviews
    };

});